package com.gmail.dmitryustimov.ms.observer.java7_innerclasses;

import java.util.ArrayList;
import java.util.List;


public class EventPublisher<T> {

    List<SubscribeAction<T>> subscriptions = new ArrayList<SubscribeAction<T>>();
    Boolean isPublicationSuspended = false;

    void subscribe(SubscribeAction<T> action) {
        subscriptions.add(action);
    }

    void unsubscribe(SubscribeAction<T> action) {
        subscriptions.remove(action);
    }

    void publish(T eventData) {
        if (!isPublicationSuspended) {
            for (SubscribeAction<T> action: subscriptions) {
                action.process(eventData);
            }
        }
    }

    void suspendPublication() {
        isPublicationSuspended = true;
    }

    void restorePublication() {
        isPublicationSuspended = false;
    }
}